/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-05-22
 * Time:14:42
 */
public class 图像渲染 {
    class Solution {
        int[][] arr = {{0,1},{1,0},{0,-1},{-1,0}};
        public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
            int oldColor = image[sr][sc];
            int row = image.length;
            int col = image[0].length;
            int book[][] = new int[row][col];
            dfs(image,row,col,book,sr,sc,oldColor,newColor);
            return image;
        }

        public void dfs(int[][] image,int row,int col,int[][] book,int sr,int sc,int oldColor,int newColor) {
            image[sr][sc] = newColor;
            book[sr][sc] = 1;
            for(int i = 0;i < 4;i++) {
                int newSr = sr + arr[i][0];
                int newSc = sc + arr[i][1];
                if(newSr >= row || newSr < 0 || newSc >= col || newSc < 0) {
                    continue;
                }
                if(image[newSr][newSc] == oldColor && book[newSr][newSc] == 0) {
                    dfs(image,row,col,book,newSr,newSc,oldColor,newColor);
                }
            }
        }
    }
}
